﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Data;

namespace AutomationAdmin.ViewModels
{
    public class AutomationAppList : INotifyPropertyChanged
    {
        private IList<AutomationApp> _automationApps;
        private PagedCollectionView _automationAppsView;
        private PropertyGroupDescription _regionGroup, _sectorGroup;
        private bool _isGroupChanged;

        public PagedCollectionView AutomationAppsView
        {
            get { return _automationAppsView; }
        }

        public IList<AutomationApp> AutomationApps
        {
            get { return _automationApps; }
        }

        public bool IsGroupChanged
        {
            get 
            {
                bool old = _isGroupChanged;
                _isGroupChanged = false;
                return old;
            }
        }
 
        public bool GroupByRegion 
        {
            get
            {
                return _regionGroup != null;
            }
            set
            {
                if (value == GroupByRegion)
                    return;

                if (value)
                {
                    _regionGroup = new PropertyGroupDescription("Region");
                    _automationAppsView.GroupDescriptions.Add(_regionGroup);
                }
                else
                {
                    _automationAppsView.GroupDescriptions.Remove(_regionGroup);
                    _regionGroup = null;
                }

                _isGroupChanged = true;
                OnPropertyChanged("GroupByRegion");
            }
        }

        public bool GroupBySector
        {
            get
            {
                return _sectorGroup != null;
            }
            set
            {
                if (value == GroupBySector)
                    return;

                if (value)
                {
                    _sectorGroup = new PropertyGroupDescription("Sector");
                    _automationAppsView.GroupDescriptions.Add(_sectorGroup);
                }
                else
                {
                    _automationAppsView.GroupDescriptions.Remove(_sectorGroup);
                    _sectorGroup = null;
                }

                _isGroupChanged = true;
                OnPropertyChanged("GroupBySector");
            }
        }

        public AutomationAppList()
        {
            _automationApps = new List<AutomationApp>();
            _automationAppsView = new PagedCollectionView(_automationApps);
        }

        public void UpdateAutomationApps(IList<AutomationApp> apps)
        {
            _automationApps = apps;
            _automationAppsView = new PagedCollectionView(_automationApps);
            OnPropertyChanged("AutomationAppsView");
        }

        public AutomationApp GetAppById(int id)
        {
            foreach (AutomationApp app in _automationApps)
            {
                if (app.Id == id)
                    return app;
            }

            return null;
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}
